package com.molyfun.parents.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.report.adapter.ReportDetailAdapter;
import com.molyfun.parents.rest.bean.ReportDetailResponse;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class ReportDetailActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.submit)
    TextView mRight;
    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.feedback_add)
    TextView mFeedbackAdd;
    @InjectView(R.id.feedback_list)
    TextView mFeedbackList;

    LinearLayoutManager mLinearLayoutManager;
    ReportDetailAdapter reportDetailAdapter;

    private String mReportId;
    private String mMsgId;
    private int mCurrentFeedBackTimes;
    private String flag;

    public static void startActivity(Context context, String reportId) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.setClass(context, ReportDetailActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String reportId, String mMsgId) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.setClass(context, ReportDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_detail);
        ButterKnife.inject(this);

        mTitleBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");

        mTitle.setText("报告详情");
        mRight.setText("报表");

        mRecyclerView.setHasFixedSize(true);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        mTitleBack.setOnClickListener(this);
        mFeedbackAdd.setOnClickListener(this);
        mFeedbackList.setOnClickListener(this);
        mRight.setOnClickListener(this);

        reportDetailAdapter = new ReportDetailAdapter(this);
        mRecyclerView.setAdapter(reportDetailAdapter);
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        mMsgId = getIntent().getStringExtra(Const.MSG_ID);
        getReportDetail(mReportId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.feedback_add:
                if (mCurrentFeedBackTimes >= 5) {
                    toast("最多五条报告反馈，已达到上限");
                    return;
                }
                ReportFeedbackEditActivity.setReportFeedbackCallback(new ReportFeedbackEditActivity.ReportFeedbackCallback() {
                    @Override
                    public void onFeedbackSuccess() {
                        getReportDetail(mReportId);
                    }

                    @Override
                    public void onFeedbackEdtSuccess(String content) {

                    }
                });
                ReportFeedbackEditActivity.startActivity(ReportDetailActivity.this, mReportId, null);


                break;
            case R.id.feedback_list:
                if (mCurrentFeedBackTimes >= 0) {
                    ReportFeedbackListActivity.startActivity(this, mReportId, reportDetailAdapter.getTimeInterval());
                }
                break;
            case R.id.submit:
                if(flag != null && flag.equals("1")) {
                    ReportFromsActivity.startActivity(this,reportDetailAdapter.getTimeInterval(),mReportId);
                }
                break;
        }
    }

    private void getReportDetail(String reportId) {

        showProgressDialog();
        Call<ReportDetailResponse> call = MFPApplication.getInstance().getRestClient().getReportDetail(reportId);
        call.enqueue(new Callback<ReportDetailResponse>() {
            @Override
            public void onResponse(Call<ReportDetailResponse> call, Response<ReportDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData().getReportcontentlist() != null
                        && response.body().getData().getReportcontentlist().size() > 0) {
                    flag = response.body().getData().getFlag();
                    if(flag != null && flag.equals("1")) {
                        mRight.setVisibility(View.VISIBLE);
                    }else {
                        mRight.setVisibility(View.GONE);
                    }
                    reportDetailAdapter.initData(response.body().getData());
                    mCurrentFeedBackTimes = response.body().getData().getFeedbacktimes();
                    if (mCurrentFeedBackTimes > 0) {
                        mFeedbackList.setTextColor(ContextCompat.getColor(ReportDetailActivity.this, R.color.color_white));
                    } else {
                        mFeedbackList.setTextColor(ContextCompat.getColor(ReportDetailActivity.this, R.color.color_white));
                    }
                }
            }

            @Override
            public void onFailure(Call<ReportDetailResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
